package com.nfit.service.impl.user;

import com.nfit.dao.AllUserMapper;
import com.nfit.dao.UserMapper;
import com.nfit.entity.User;
import com.nfit.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private AllUserMapper allUserMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;

    @Override
    public User login(User user) {
        if (redisTemplate.opsForValue().get(user.getEmail()).equals(user.getCode())) {
            User login = userMapper.login(user);
            redisTemplate.opsForValue().set(user.getEmail(), String.valueOf(login.getUserId()));
            return login;
        }
        return null;
    }

    @Override
    public Boolean register(User user) {
        if (userMapper.login(user) == null) {
            user.setUserId(userMapper.register(user));
            allUserMapper.insertUser(user);
            return true;
        }
        return false;
    }

    @Override
    public User selectUserById(String userId) {
        return userMapper.selectUserById(userId);
    }

    @Override
    public int updateUserInfo(User user) {
        return userMapper.updateUserInfo(user);
    }
}
